package socket

import (
	"context"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gorilla/websocket"
	"net/http"
)

var (
	upGrader = websocket.Upgrader{
		ReadBufferSize:  1024,
		WriteBufferSize: 1024,
		CheckOrigin: func(r *http.Request) bool {
			return true
		},
	}
	clientManage = NewSocketManager()
)

func StartWebsocket(ctx context.Context) {
	go clientManage.Start()
}
func RegisterWebsocket(r *ghttp.Request) {
	conn, err := upGrader.Upgrade(r.Response.ResponseWriter, r.Request, nil)
	if err != nil {
		panic(err)
	}
	param := r.Get("id")
	if param.IsNil() || param.IsEmpty() {
		panic("id is nil")
		return
	}
	client := NewClient(r.GetRemoteIp(), conn, param.String())
	go client.Read()
	go client.Write()
	clientManage.Register <- client

}
